#include "stopwatchview.h"

StopWatchView::StopWatchView(QWidget *parent)
    : QWidget{parent}
{
    //定时器动态增加时速
    myTimer = new QTimer(this);
    connect(myTimer,SIGNAL(timeout()),this,SLOT(slot_speed_changed()));

    setUI();



}
void StopWatchView::setUI()
{
    QWidget *bottom = new QWidget(this);
    bottom->setGeometry(0,292,350,70);

    //水平布局
    hLayout = new QHBoxLayout();
    hLayout->setSpacing(0);
    hLayout->setContentsMargins(0,0,0,0);
    bottom->setLayout(hLayout);

    targetSpeedLabel = new QLabel();
    targetSpeedLabel->setText("0\nkm/hr");
    targetSpeedLabel->setAlignment(Qt::AlignCenter);
    targetSpeedLabel->setStyleSheet("font-size:12px;color:rgba(0,153,61, 100%);");
    targetSpeedLabel->setFixedSize(45,45);


    operationalLabel = new QLabel();
    operationalLabel->setText("CM");
    operationalLabel->setAlignment(Qt::AlignCenter);
    operationalLabel->setStyleSheet("font-size:22px;color:white;font-weight: bold;");
    operationalLabel->setFixedSize(135,45);

    departLabel = new QLabel();
    departLabel->setFixedSize(45,45);
    departLabel->setPixmap(QPixmap(":images/train_depart_green.png"));
    departLabel->setScaledContents(true);


    hLayout->addWidget(targetSpeedLabel,45);

    hLayout->addWidget(operationalLabel,135);
    hLayout->addWidget(departLabel,45);





}
void StopWatchView::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    int width=this->width();
    int height=this->height();//移动仪表盘的高度
    int radius=((width>height)?height:width)/1.6;//仪表盘的中心位置
    //移动画笔到中下方
    painter.translate(width/2,height/2);
    //启用反锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    //设置画刷颜色
    painter.setBrush(QColor(138,43,226));
    DrawSmallScale(painter,radius-60);//刻度线
    DrawDigital(painter,radius-80);//刻度数字
    /*所有形状绘画*/
    DrawPointer(painter,radius-130);//指针


    DrawNum(painter,radius-280);//时速
}


//绘制外圈点
void StopWatchView::DrawPoint(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(-2,-2);
    pointPath.lineTo(2,-2);
    pointPath.lineTo(2,2);
    pointPath.lineTo(0,4);
    pointPath.lineTo(-2,2);
    //绘制13个小点
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        painter.setBrush(QColor(250,252,78));
        //计算并移动绘图对象中心点
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        //计算并移动绘图对象的中心点
        painter.translate(point.x(),-point.y());
        //计算并选择绘图对象坐标
        painter.rotate(-120+i*20);
        //绘制路径
        painter.drawPath(pointPath);
        painter.restore();
    }
}

void StopWatchView::DrawDigital(QPainter& painter,int radius)
{
    //设置画笔，画笔默认NOPEN
    painter.setPen(QColor(255,255,255));
    QFont font;
    font.setFamily("Arial");
    font.setPointSize(15);
    font.setBold(true);
    painter.setFont(font);
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*20);
        painter.drawText(-25, 0, 50, 20,Qt::AlignCenter,QString::number(i*10));
        painter.restore();
    }
    //还原画笔
    painter.setPen(Qt::NoPen);
}



//绘制刻度
void StopWatchView::DrawSmallScale(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath_small;
    pointPath_small.moveTo(-0.5,-0.5);
    pointPath_small.lineTo(0.5,-0.5);
    pointPath_small.lineTo(0.5,10);
    pointPath_small.lineTo(-0.5,10);


    QPainterPath pointPath_big;
    pointPath_big.moveTo(-0.5,-0.5);
    pointPath_big.lineTo(0.5,-0.5);
    pointPath_big.lineTo(0.5,15);
    pointPath_big.lineTo(-0.5,15);

    //绘制121个小点
    for(int i=0;i<121;i+=1){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*2)*M_PI)/180));
        point.setY(radius*qSin(((210-i*2)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*2);

        painter.setBrush(QColor(255,255,255));

        if(i%5 == 0)
        {
            painter.drawPath(pointPath_big);//绘画大刻度
        }else
        {
            painter.drawPath(pointPath_small);//绘画小刻度
        }
        painter.restore();
    }
}

//绘制实时时速数字
void StopWatchView::DrawNum(QPainter& painter,int radius)
{
    painter.save();
    painter.setPen(QColor(255,255,255));
    QFont font;
    font.setFamily("Arial");
    font.setPointSize(45);
    painter.setFont(font);
    painter.drawText(-75, -radius - 30, 150, 100,Qt::AlignCenter,QString::number(degRotate));
    painter.restore();
}

//绘制指针
void StopWatchView::DrawPointer(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    QPainterPath inRing;
    inRing.addEllipse(-5,-5,10,10);
    painter.save();

    //计算并选择绘图对象坐标
    painter.rotate(degRotate - 120);
    painter.setBrush(QColor(255,255,255));
    painter.drawPath(pointPath.subtracted(inRing));
    painter.restore();
}

void StopWatchView::slot_speed_changed()
{
    if(direction == 1)//加速
    {
        degRotate++;
        if(degRotate > 240)
            degRotate = 240;
    }
    else if(direction == 0)//减速
    {
        degRotate--;
        if(degRotate < 0)
        {
            degRotate = 0;
            myTimer->stop();
        }
    }
    QString s = QString::number(degRotate) + "\nkm/hr";
    targetSpeedLabel->setText(s);
    update();//刷新画面。很重要！
}


